package com.example.backend.controller;


import com.example.backend.mapper.RestaurantMapper;
import com.example.backend.pojo.Foodorder;
import com.example.backend.pojo.Orderdetail;
import com.example.backend.pojo.Restaurant;
import com.example.backend.pojo.Result;
import com.example.backend.service.OrderService;
import com.example.backend.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RestaurantMapper restaurantMapper;

    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Foodorder foodorder){

        orderService.addOrder(foodorder);
        Restaurant r = restaurantMapper.getRestaurantById(foodorder.getRestaurantid());
        restaurantMapper.surplus(r.getRestaurantid(), r.getSurplus()-1);
        restaurantMapper.updatesales(r.getSales()+1,r.getRestaurantid());
        return  Result.success();
    }

    @GetMapping("/getuserorder")
    public Result<List<Foodorder>> getuserorder(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        return  Result.success(orderService.getuserorder(id));
    }


    @PostMapping("/getresorder")
    public Result<List<Foodorder>> getresorder(Integer state){
        Map<String,Object> map = ThreadLocalUtil.get();
        String id = (String) map.get("id");
        return  Result.success(orderService.getresorder(id,state));
    }



    @GetMapping("/getuserorder0")
    public Result<List<Foodorder>> getuserorder0(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        return  Result.success(orderService.getuserorderx(id,0));
    }
    @GetMapping("/getuserorder1")
    public Result<List<Foodorder>> getuserorder1(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        return  Result.success(orderService.getuserorderx(id,1));
    }
    @GetMapping("/getuserorder2")
    public Result<List<Foodorder>> getuserorder2(){
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        return  Result.success(orderService.getuserorderx(id,2));
    }

    @PostMapping("/getuserorderdetail")
    public Result<List<Orderdetail>> getuserorderdetail(Integer id){
        return  Result.success(orderService.getuserorderdetail(id));
    }
    @PostMapping("/findbyId")
    public Result<Foodorder> findbyId(Integer orderid){
        return Result.success(orderService.findbyId(orderid));
    }

}
